package server.model.goodModel;


import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import server.db.DBDriver;
import server.db.DBFactory;
import server.entity.Good;
import server.util.InfoUtils.Info;
import server.util.InfoUtils.InfoEnum;
import server.util.InfoUtils.InfoUtil;
import server.util.MyUtils;

import java.util.ArrayList;
import java.util.List;

public class GoodInfoModel {
    public Info getGoodInfo(int goodId) {
        String sql = "select * from good where good_id = ? limit 1";
        Good good = DBFactory.getDBDriver().query(sql, new BeanHandler<>(Good.class, DBDriver.getProcess()), goodId);
        if (good != null) {
            return InfoUtil.getInfo(good);
        } else {
            return InfoUtil.getInfo(1,"不存在该商品，或已售空、删除");
        }
    }

    //获取所有已上架的商品Id
    public Info getGoodList() {
        String sql = "select good_id from good where status = 1";
        List<Good> goods = DBFactory.getDBDriver().query(sql, new BeanListHandler<>(Good.class, DBDriver.getProcess()));
        if (goods != null) {
            ArrayList<Integer> goodIds=new ArrayList<>();
            for(Good good:goods){
                goodIds.add(good.getGoodId());
            }
            return InfoUtil.getInfo(goodIds);
        } else {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        }
    }


    public Info setGoodInfo(Good good) {
        Info info = null;
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("update good set ");

        String goodName = good.getGoodName();
        if (MyUtils.NOTNULL(goodName)) {
            sql.append(" good_name = ?, ");
            params.add(goodName);
        }

        String pic = good.getPic();
        if (MyUtils.NOTNULL(pic)) {
            sql.append(" pic = ?, ");
            params.add(pic);
        }

        Double price = good.getPrice();
        if (price != null) {
            price = MyUtils.formatDouble(price);
            sql.append(" price = ?, ");
            params.add(price);
        }

        Integer category = good.getCategory();
        if (category != null) {
            sql.append(" category = ?, ");
            params.add(category);
        }

        Integer count = good.getCount();
        if (count != null) {
            sql.append(" count = ?, ");
            params.add(count);
        }


        String description = good.getDescription();
        if (MyUtils.NOTNULL(description)) {
            sql.append(" description = ?, ");
            params.add(description);
        }

        Integer auctionId = good.getAuctionId();
        if (auctionId != null) {
            sql.append(" auction_id = ?, ");
            params.add(auctionId);
        }

        //设置选择条件
        sql.append(" where good_id = ? limit 1 ");
        params.add(good.getGoodId());

        //去除最后一个“,”
        String endSql = sql.deleteCharAt(sql.lastIndexOf(",")).toString();

        //操作数据库影响的行数
        int line = DBFactory.getDBDriver().update(endSql, params.toArray());

        if (line > 0) {
            info = InfoUtil.getInfo(InfoEnum.SUCCESS);
        } else {
            info = InfoUtil.getInfo(InfoEnum.FAIL);
        }
        return info;
    }


    public Info GoodNumDecrease(int goodId, int count) {
        Good good = (Good) getGoodInfo(goodId).getData();
        if (good != null && good.getCount() != null) {
            if (good.getCount() >= count) {
                String sql = "update good set count = count - ? where good_id = ?";
                if (DBFactory.getDBDriver().update(sql, count, goodId) > 0) {
                    //info 里设置需要的金额
                    return InfoUtil.getInfo(count * good.getPrice());
                }
            } else
                return InfoUtil.getInfo(1, "货源不足，仅剩余" + good.getCount() + "件");
        }
        return InfoUtil.getInfo(InfoEnum.FAIL);
    }

}
